jQuery(document).ready(function(){
    wsocket.onmessage = function(ev) {
    var msg = JSON.parse(ev.data); //chuyen chuoi JSON thanh doi tuong JSON
    var type = msg.type; //kieu message
    var user_id =msg.receiver_id;

    if (user_id== notify_obj.current_user) //neu la message cua user
        {
            jQuery.ajax({
                type: "POST",
                url: notify_obj.ajaxurl,
                data: {
                    action: "get_counter",
                    receiver_id: user_id,
                    notification_type:type,
                },
                success: function(data) {

                    var json = JSON.parse(data);
                    var counter=json.counter;

                    if(type == 'like')
                    {
                        jQuery('.like_counter').css({opacity: 0});
                        jQuery('.like_counter').text(counter);
                        jQuery('.like_counter').css({top: '-10px'});
                        jQuery('.like_counter').animate({top: '-2px', opacity: 1});
                        //hieu ung am thanh
                        playNotificationSound();
                        
                    }
                    else if(type == 'comment') //neu la message cua he thong
                    {


                        jQuery('.comment_counter').css({opacity: 0});
                        jQuery('.comment_counter').text(counter);
                        jQuery('.comment_counter').css({top: '-10px'});
                        jQuery('.comment_counter').animate({top: '-2px', opacity: 1});
                        
                        
                    }
                    else if(type == 'image') //neu la message cua he thong
                    {
                        jQuery('.image_counter').css({opacity: 0});
                        jQuery('.image_counter').text(counter);
                        jQuery('.image_counter').css({top: '-10px'});
                        jQuery('.image_counter').animate({top: '-2px', opacity: 1});
                    }
                    else if(type == 'message') //neu la message cua he thong
                    {
                        jQuery('.image_counter').css({opacity: 0});
                        jQuery('.image_counter').text(counter);
                        jQuery('.image_counter').css({top: '-10px'});
                        jQuery('.image_counter').animate({top: '-2px', opacity: 1});
                    }
                }
            });
           
        }
        
        
    };
    jQuery("#btn_like").hover(function()
    {

         jQuery.ajax({
                type: "POST",
                url: notify_obj.ajaxurl,
                data: {
                    action: "get_liked_list",
                },
                success: function(data) {
                    // var myWindow = window.open("", "MsgWindow", "width=1000, height=1000");
                    // myWindow.document.write(data);
                    jQuery("#liked_list").html(data);
                    jQuery("#liked_list a").addClass("liked_list_add");
                    jQuery("#liked_list li").addClass("liked_list_li_add");
                    jQuery('.like_counter').css({opacity: 0});

                }
            });
    });
    jQuery("#btn_comment").hover(function()
    {

         jQuery.ajax({
                type: "POST",
                url: notify_obj.ajaxurl,
                data: {
                    action: "get_commented_list",
                },
                success: function(data) {
                    // var myWindow = window.open("", "MsgWindow", "width=1000, height=1000");
                    // myWindow.document.write(data);
                    jQuery("#commented_list").html(data);
                    jQuery("#commented_list a").addClass("liked_list_add");
                    jQuery("#commented_list li").addClass("liked_list_li_add");
                    jQuery('.comment_counter').css({opacity: 0});
                }
            });
    });

});
function playNotificationSound()
{
    var audio = {};
    audio["walk"] = new Audio();
    audio["walk"].src = notify_obj.current_url;
    audio["walk"].addEventListener('load', function () {
    audio["walk"].play();
    });
}
